package com.hanthink.web.controller.app.user;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.user.User;
import com.hanthink.system.domain.user.UserClockInLog;
import com.hanthink.system.service.user.IUserService;

@RestController
@RequestMapping("/app/user/userInfo")
public class UserControll extends BaseController {
	@Autowired
	private IUserService userService;
	
	@Autowired
	private TokenService tokenService;

	@PostMapping("/list")
	public TableDataInfo list(@RequestBody User user) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			user.setSiteKey(loginUser.getUser().getSite());
		}
		startPage();
		List<User> list = userService.selectUserList(user);
		return getDataTable(list);
	}

    @PostMapping("/listOfAutoDistingguish")
    public TableDataInfo listOfAutoDistingguish(@RequestBody User user) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
            user.setSiteKey(loginUser.getUser().getSite());
        }
        startPage();
        List<User> list = userService.selectUserListOfAutoDistingguish(user);
        return getDataTable(list);
    }

	@PostMapping("/repairUserlist")
	public TableDataInfo repairUserlist(@RequestBody User user) {
		
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			user.setSiteKey(loginUser.getUser().getSite());
		}
		startPage();
		List<User> list = userService.selectRepairUserList(user);
		return getDataTable(list);
	}

	@PostMapping("/repairUserlistsj")
	public TableDataInfo repairUserlistsj(@RequestBody User user) {

		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			user.setSiteKey(loginUser.getUser().getSite());
		}
		startPage();
		List<User> list = userService.selectRepairUserListsj(user);
		return getDataTable(list);
	}


	@PostMapping("/repairUserlistNoClockin")
	public TableDataInfo repairUserlistNoClockin(@RequestBody User user) {
		startPage();
		List<User> list = userService.selectRepairUserListNoClockin(user);
		return getDataTable(list);
	}

	@PostMapping("/status1")
	public TableDataInfo selectStatusByUser(@RequestBody User user) {
		User info = userService.selectStatusByUser(user);

		TableDataInfo tableDataInfo = new TableDataInfo();
		List<User> infos = new ArrayList<User>();
		infos.add(info);
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("操作成功");
		tableDataInfo.setRows(infos);
		return tableDataInfo;
	}

	/**
	 * 更新用户打卡记录
	 */
	@PostMapping("/updateStatus")
	public AjaxResult updateUserStatus(@RequestBody User user) {

		boolean result = userService.updateUserStatus(user);

		if (!result) {
			return AjaxResult.error("User更新失败！");
		}
		UserClockInLog userClockInLog = new UserClockInLog();
		userClockInLog.setUserId(user.getUserKey());
		if ("1".equals(user.getClockin())) {
			userClockInLog.setCurrentStatus("0");
		} else {
			userClockInLog.setCurrentStatus("1");
		}
		userClockInLog.setUpdatedStatus(user.getClockin());
		userClockInLog.setOperationType("0");
		userService.insertUserClockInLog(userClockInLog);
		return AjaxResult.success();
	}
}
